<?php
/**
 * SoleController.class.php
 * 分销控制器
 *
 * @author  王中艺
 * @date    2016-11-21
 */

namespace Wechat\Controller;

class SoleController extends BaseController{

    /**
     * 获取下线用户
     * @param   type    下线用户类型
     * @param   page
     * @return  json
     */
    function childUsers(){
        $type   = $this->iInfo['type'];
        $page   = $this->iInfo['page'] ? $this->iInfo['page'] : 1;

        $dUser  = D('User');
        $result     = $dUser->getChildUsers(C('user'), $type, $page, $count);
        if($result === false)
            $this->outOInfo($dUser->errMsg, 0);

        $this->outOInfo(array('list' => $result, 'count' => $count));
    }

    /**
     * 获取用户下线发展业绩
     * @param   $child_role_id 下线角色ID
     * @param   $page 页码数
     * @param   $time_start 开始时间
     * @param   $time_end 结束时间
     * @param   $keyword 关键词
     * @return   json
     */
    function childGpa(){
        $this->unEmptyParam(array('child_role_id'));

        $cRoleId    = $this->iInfo['child_role_id'];
        $page   = $this->iInfo['page'] ? $this->iInfo['page'] : 1;
        $timeS  = $this->iInfo['time_start'] ? $this->iInfo['time_start'] : strtotime(date('Y-m-1'));
        if($timeS && !is_numeric($timeS))
            $timeS  = strtotime($timeS);
        $timeE  = $this->iInfo['time_end'] ? $this->iInfo['time_end'] : strtotime(date('Y-m-1', $timeS).' +1 month');
        if($timeE && !is_numeric($timeE))
            $timeE  = strtotime($timeE);

        $result     = D('User')->childGpa(C('user'), $cRoleId, $page, $timeS, $timeE, $this->iInfo['keyword']); 
        if($result === false)
            $this->outOInfo(D('User')->errMsg, 0);
        $this->outOInfo(array('list' => $result));
    }

    /**
     * 销售记录
     * @param   page
     */
    function soleLog(){
        $page   = $this->iInfo['page'] ? $this->iInfo['page'] : 1;
        $keyword    = $this->iInfo['keyword'] ? $this->iInfo['keyword'] : false;

        $dUser  = D('user');
        $result     = $dUser->getSoleLog(C('user.id'), $page, $keyword);
        if($result === false)
            $this->outOInfo($dUser->errMsg, 0);

        $this->outOInfo(array('list' => $result));
    }

    /**
     * 收益详情
     * @return  json
     */
    function deductDetail(){
        $dateS  = $this->iInfo['date_start'] ? $this->iInfo['date_start'] : date('Y-m-1');
        $dateE  = $this->iInfo['date_end'] ? $this->iInfo['date_end'] : date('Y-m-d',strtotime(date('Y-m-1').' +1 month'));
        $timeS  = strtotime($dateS);
        $timeE  = strtotime($dateE);
        
        $dUser  = D('User');
        $result = $dUser->deductDetail(C('user.id'), $timeS, $timeE);
        if($result === false){
            $this->outOInfo($dUser->errMsg, 0);
        }
        $result['amount']   = C('user.amount');
        $result['wallet']   = C('user.wallet');
        $this->outOInfo(array('info' => $result));
    }

    /**
     * 提现
     * @param   amount  提现金额
     * @return  userInfo    用户信息
     */
    function withdraw(){
        $this->unEmptyParam(array('amount'));
        $amount     = $this->iInfo['amount'];

        //判断用户是否完善相关信息
        $soleInfo   = D('Sole')->getSoleInfo(C('user.id'));
        if(!$soleInfo){
            $this->outOInfo('请完善相关的用户信息!', 300);
        }

        $dWD  = D('Withdraw');
        $result     = $dWD->withdraw(C('user.id'), $amount);
        if($result === false){
            $this->outOInfo($dWD->errMsg, 0);
        }
        $this->outOInfo(array('user_info' => $result));
    }

    /**
     * 提现记录
     * @param   page
     */
    function withdrawLog(){
        $page   = $this->iInfo['page'] ? $this->iInfo['page'] : 1;

        $dWd    = D('Withdraw');
        $result     = $dWd->withdrawLog(C('user.id'), $page);
        if($result === false)
            $this->outOInfo($dWd->errMsg, 0);

        $this->outOInfo(array('list' => $result));
    }

    /**
     * 更新销售人员信息
     * @param   $truename 真实姓名
     * @param   $mobile 手机号
     * @param   $qq QQ号
     * @param   $region 所在区域
     * @param   $school_name 幼儿园名称
     * @param   $school_students 学生数量
     * @param   $school_teachers 教师数量
     */
    function updateSoleInfo(){
        //检测必须参数
        $this->unEmptyParam([
            'truename', 'mobile', 'qq', 'region'
        ]);

        $result     = D('Sole')->updateSoleInfo(
            C('user.id'),
            $this->iInfo['truename'],
            $this->iInfo['mobile'],
            $this->iInfo['qq'],
            $this->iInfo['region'],
            $this->iInfo['school_name'],
            $this->iInfo['school_students'],
            $this->iInfo['school_teachers'],
            $this->iInfo['school_stu_fee']
        );
        if($result === false)
            $this->outOInfo(D('Sole')->errMsg, 0);
        $this->outOInfo('更新成功!', 1);
    }

    /**
     * 获取销售人员额外信息
     * @return   json
     */
    function getSoleInfo(){
        $info   = D('Sole')->getSoleInfo(C('user.id'));
        if(!$info)  $info   = array();
        $info   = C('user') + $info;
        $this->outOInfo(array('info' => $info));
    }
}